package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.RichTextEditor;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class NewMessage{
		private var panel:SuperPanel = new SuperPanel();
		private var newMessageBtn:Button = new Button();
		private var tekstEditor:RichTextEditor = new RichTextEditor();
		private var naslov:TextInput = new TextInput();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var tool:Tool = new Tool();
		private var primaoc:TextInput = new TextInput();
		private var labela:Label = new Label();
		
		public function NewMessage(p:SuperPanel){
			panel = p;
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			newMessageBtn.label = "POSALJI";
			newMessageBtn.x = panel.width-95;
			newMessageBtn.y = panel.height-42;
			primaoc.x = 70;
			primaoc.y = panel.height-42;
			primaoc.height = 20;
			primaoc.width = panel.width-210;
			labela.x=10;
			labela.y=panel.height-42;
			labela.text = "Primaoc:";
			newMessageBtn.addEventListener(MouseEvent.CLICK, onNewMessageBtnClick);
			naslov.x = 10;
			naslov.y = 40;
	 		naslov.height=20;
	 		naslov.width=panel.width-30;
	 		naslov.toolTip = "Unesite naslov poruke";
			tekstEditor.x = 10;
			tekstEditor.y = 60;
	 		tekstEditor.height=panel.height-111;
	 		tekstEditor.width=panel.width-30;
			tekstEditor.title="Unesite tekst poruke: ";
			tekstEditor.setStyle("headerHeight", 20);
			tekstEditor.setStyle("borderThicknessTop", 0);
			tekstEditor.setStyle("borderThicknessBottom", 0);
			tekstEditor.setStyle("borderThicknessLeft", 0);
			tekstEditor.setStyle("borderThicknessRight", 0);
			tekstEditor.setStyle("borderAlpha", 0);
			tekstEditor.setStyle("dropShadowEnabled", false);
			p.addChild(newMessageBtn);
			p.addChild(tekstEditor);
			p.addChild(naslov);
			p.addChild(primaoc);
			p.addChild(labela);
		}
		
		public function onNewMessageBtnClick(e:Event):void{
			if(tekstEditor.text.length>0&&naslov.text.length>0&&primaoc.text.length>0){
	            service.sql = {type:"SELECT", a1:"k.id_username", from:"korisnik k", where:"k.username=@"+primaoc.text+"@" }; 
				service.addEventListener(service.MYSQL_RESULT, onCheckPrimaoc);
 				service.startService();
 			} else tool.createAlert("Greska!", "Morate unijeti tekst, naslov poruke i primaoca!", 200);
		}
		
		public function onCheckPrimaoc(e:Event):void{
	 		var xmlList:XMLList = new XMLList();
			xmlList = service.xml.children();
	 		if(xmlList.length()>0){
				var help:String=tekstEditor.htmlText;
				if(tool.checkForCRC(help)){
					tool.createAlert("Greska!", "Rijec 'CRC' se ne smije nalaziti u tekstu!", 200);
				} else {
					help=tool.CodeTekst(tekstEditor.htmlText);
            		service1.sql = {type:"INSERT", into:"privatnaporuka", columns:"naslov, posiljaoc, primaoc, poruka, procitana, datumSlanja, vrijemeslanja", values:"@"+naslov.text+"@,@"+panel.parentApplication.username+"@,@"+xmlList.id_username+"@,@"+help+"@,@"+0+"@,@"+tool.currentDate()+"@,@"+tool.currentTime()+"@" }; 
					service1.addEventListener(service1.MYSQL_RESULT, onResult);
 					service1.startService();
 				}
 			} else tool.createAlert("Greska!", "Username primaoca ne postoji u bazi!", 200);
		}
		
		public function onResult(e:Event):void{
			if(service.xml.results.result){
				tool.createAlert("Info", "Poruka je uspjesno poslata!", 200);
				panel.closePanel();
			} else tool.createAlert("Info", "Pri slanju teme dogodila se greska, provjerite da li ste ispravno upisali primaoca i pokusajte ponovo!", 200);
		}
		
		public function onResize(e:Event):void{
			newMessageBtn.x = panel.width-95;
			newMessageBtn.y = panel.height-42;
	 		naslov.width=panel.width-30;
	 		tekstEditor.height=panel.height-111;
	 		tekstEditor.width=panel.width-30;
			primaoc.width = panel.width-210;
			primaoc.y = panel.height-42;
			labela.y=panel.height-42;
		}
		
		public function set defaultPrimaoc(s:String):void{
			primaoc.text = s;
		}
	}
}